#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "toolbox2.h"

using namespace std;

bool checkFile(char * file_name);
bool get_mono_sequence(char *& file_name);
bool get_search_sequence(char *& file_name);
int get_option();
bool get_range(int &min, int &max);


int main_console()
{
	char response;
	int option;
	char *monoFile=new char [300];
	char *seqFile=new char [300];
	bool run=true;
	Toolbox2 * t;

	while(run)
	{
		option=get_option();
		switch(option)
		{
			case 1:
			{
				get_mono_sequence(monoFile);
				get_search_sequence(seqFile);
				t=new Toolbox2(monoFile,seqFile);
				delete t;
				cout << endl;
				cout << "Your files are written.\nWould you like analyze another sequence? (Y/N) ";
				cin >> response;
				cin.clear();
				if( (response=='n') || (response=='N') ) run=false;
				break;
			}
			case 2:
			{
				get_search_sequence(seqFile);
				t=new Toolbox2(seqFile);
				delete t;
				cout << endl;
				cout << "Your files are written.\nWould you like analyze another sequence? (Y/N) ";
				cin >> response;
				cin.clear();
				if( (response=='n') || (response=='N') ) run=false;
				break;
			}
			case 3:
			{
				run=false;
				cout << "Goodbye!\n";
				break;
			}
			default:
			{
				cout << "'" << option << "' is not a valid option! Try again.\n";
				system("pause");
				run=true;
				break;
			}
		}
	}
	return 1;
}


bool checkFile(char * file_name)
{
        //returns false if the file already exists
    //cbun: had to change this because ios::nocreate is deprecated
    // and not available in <fstream>
        ifstream in;
        in.open(file_name,ios::in);
        if(!in)
        {
                in.clear();
                in.close();
                return true;
        }
        else
        {
                in.clear();
                in.close();
                return false;
        }
}

int get_option()
{
	int option;
	bool ask=true;
	system("cls");
	while(ask)
	{
		cout << "Welcome to MiIP\nOptions:\n";
		cout << "1. Monomer Search - supply monomer & search sequences\n";
		cout << "2. Monomer Discovery - supply a search sequence and range for monomer length\n";
		cout << "3. Quit\n";
		cout << "Enter your choice: ";
		cin>>option;
		cin.clear();
		if( (option==1) || (option==2) || (option==3) )
			return option;
		else
		{
			system("cls");
			cout << "That is not a valid choice.\n\n";
		}
	}
}

bool get_mono_sequence(char *& file_name)
{
	cout << endl;
	cout << "Please enter the name of the FASTA file containing the monomer sequence:\n";
	cin>>file_name;
	cin.clear();

	while(!checkFile(file_name))
	{
		cout << "That file does not exist. Please try again.\n";
		cout << "Please enter the name of the FASTA file containing the monomer sequence:\n";
		cin>>file_name;
		cin.clear();
	}
	return true;
}

bool get_search_sequence(char *& file_name)
{
	cout << "Please enter the name of the FASTA file containing the search sequence:\n";
	cin>>file_name;
	cin.clear();

	while(!checkFile(file_name))
	{
		cout << "That file does not exist. Please try again.\n";
		cout << "Please enter the name of the FASTA file containing the search sequence:\n";
		cin>>file_name;
		cin.clear();
	}
	return true;
}





////////debugging functions
int main_debug()
{
	//test Array_align(Array_align * x)
	Array_align * a=new Array_align(3);
	Array_align * x=new Array_align(5);

	Align * w=new Align(1,2,4,5,1,0.3);
	a->add_align(w);
	delete w;
	w=new Align(6,7,14,15,1,0.7);
	a->add_align(w);
	delete w;
	w=new Align(12,20,40,48,9,0.9);
	a->add_align(w);
	delete w;

	cout << "a" << endl;
	a->show_align();

	w=new Align(7,8,11,12,1,0.9);
	x->add_align(w);
	delete w;
	w=new Align(16,17,4,5,1,0.2);
	x->add_align(w);
	delete w;
	w=new Align(22,30,30,38,9,0.5);
	x->add_align(w);
	delete w;
	w=new Align(1,7,1,7,7,0.8);
	x->add_align(w);
	delete w;
	w=new Align(52,60,10,18,9,0.1);
	x->add_align(w);
	delete w;

	cout << endl;
	cout << "x" << endl;
	x->show_align();

	cout << endl;
	cout << "a replaced by x" << endl;
	delete a;

	a=NULL;
	a=new Array_align(x);

	a->show_align();
	cout << endl;


	return 1;
}
